//index.js
// 导入时间js
const util = require('../../utils/util.js');
const API = require('../../utils/ajax.js');
const QQMapWX = require('../../utils/qqmap-wx-jssdk.js')
//获取应用实例
const app = getApp()

 function getDateStr (AddDayCount) {
  var dd = new Date();
  dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
  var y = dd.getFullYear();
  var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
  let nowDate=`${y}-${m}-${d}`;
  return nowDate;
}
function getDateFont(AddDayCount){
  var dd = new Date();
  dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
  var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
  let nowDate=`${m}月${d}日`
  return nowDate
}



Page({
  data: {
    navData: [
      { text: getDateFont(0), date: getDateStr(0) },
      { text: getDateFont(-1), date: getDateStr(-1) },
      { text: getDateFont(-2), date:  getDateStr(-2) },
      { text: getDateFont(-3), date: getDateStr(-3) },
      { text: getDateFont(-4), date: getDateStr(-4) },
      { text: getDateFont(-5), date: getDateStr(-5) },
      { text: getDateFont(-6), date: getDateStr(-6) },
      { text: getDateFont(-7), date: getDateStr(-7) },
      { text: getDateFont(-8), date: getDateStr(-8) },
      { text: getDateFont(-9), date: getDateStr(-9) },
      { text: getDateFont(-10), date: getDateStr(-10) },
      { text: getDateFont(-10), date: getDateStr(-11) },
      { text: getDateFont(-11), date: getDateStr(-12) },
      { text: getDateFont(-13), date: getDateStr(-13) },
      { text: getDateFont(-14), date: getDateStr(-14) },
      { text: getDateFont(-15), date: getDateStr(-15) },
      { text: getDateFont(-16), date: getDateStr(-16) },
      { text: getDateFont(-17), date: getDateStr(-17) },
      { text: getDateFont(-18), date: getDateStr(-18) },
      { text: getDateFont(-19), date: getDateStr(-19) },
      { text: getDateFont(-20), date: getDateStr(-20) },
      { text: getDateFont(-21), date: getDateStr(-21) },
      { text: getDateFont(-22), date: getDateStr(-22) },
      { text: getDateFont(-23), date: getDateStr(-23) },
      { text: getDateFont(-24), date: getDateStr(-24) },
      { text: getDateFont(-25), date: getDateStr(-25) },
      { text: getDateFont(-26), date: getDateStr(-26) },
      { text: getDateFont(-27), date: getDateStr(-27) },
      { text: getDateFont(-28), date: getDateStr(-28) },
      { text: getDateFont(-29), date: getDateStr(-29) },
    ],
    carInfo: [

    ],//车辆历史报警数据
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    currentTab: 0,
    navScrollLeft: 0,
    vid:'',
    allDayTrack:[],//全天轨迹的数据,
    trackList:[
    ],
    nowTime:'',
    transMission: null,
    loaction: '',
    subTrackList:[],
    
  },


  
  //事件处理函数
  onLoad: function (option) {
    let vid=option.vid;
    this.setData({
      vid
    })
    this.getAllDayTrackData();
    this.getSubTrackList()
    this.init()
    this.initTime();
    
  },
  switchNav(event) {
    var date = event.currentTarget.dataset.date;
    var cur = event.currentTarget.dataset.current;
    this.getSubTrackList(date);
    this.getAllDayTrackData(date);
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth / 5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    
  },
  init() {
    // 调用函数时，传入new Date()参数，返回值是日期和时间
    var time = util.formatTime(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      time: time
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })

    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
  },
 
  initTime(Num) {
    // 调用函数时，传入new Date()参数，返回值是日期和时间
    let time=util.formatTime(new Date())
    let nowTime = time.yyyy_mm_dd_hh_mm_ss;
    let nowYear=time.yyyy;
    let nowMonth=time.month;
    let nowDay=time.dd-Num;
    let nowDate=`${nowYear}-${nowMonth}-${nowDay}`
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      nowTime: nowTime,
      nowDate
    });
    return nowDate
  },
 
  async getAllDayTrackData(date){
    date=date||'2020-09-09';
    let {vid}=this.data
    const res=await API.getAllDayTrack(vid,date)
    let allDayTrack=[]
    if(res.code === 0){
      const _allDayTrack=res.data
      allDayTrack=allDayTrack.concat(_allDayTrack)
      this.setData({
        allDayTrack
      })
    }
    
  },

  async getSubTrackList(date){
    date=date||'2020-09-09';
    let {vid}=this.data;
    const res=await API.getSubTrack(vid,date)
    let subTrackList=[]
    if(res.code===0){
      let _subTrackList=res.data;
      subTrackList=subTrackList.concat(_subTrackList)
      this.setData({
        subTrackList
      })
    }
    

    let {transMission}=app.globalData;
    this.setData({
      transMission
    })
    let qqMapSDK = new QQMapWX({
      key: 'EWDBZ-3BZ36-BDGSG-MMQHU-KW7OK-FDBMA'
    });
    let that=this
    let {endLat,endLon,startLat,startLon}=subTrackList[0]||{};
    qqMapSDK.reverseGeocoder({
      location: {
        latitude: endLat,
        longitude: endLon 
      },
      success: function (addressRes) {
        // console.log(addressRes.result.address)
        that.setData({
          loation: addressRes.result.address,
        })
      }
    })

  },
  toTrackVideo:function(e){
    let {vid}=this.data;
    let url=`/pages/trackReplayVideo/trackReplayVideo?vid=${vid}`
    wx.navigateTo({
      url
    })
  },
  
})